define(['app/mdSheQuHospitalBase/bill/bill.model', 'app/mdSheQuHospitalBase/bill/bill.service', 'hr.common'
, 'app/hrManagerRbac/dictionary/dictionary.service'
], function (Model, service, common
, dictionaryService
) {
    function initialize(){
        var model = new Model();
        var initParam = model.tableModel.param();

        dictionaryService.findAll('search_EQ_parent.id='+autoConfig.bill.payType).success(function(data){
            model.dialogModel.payTypeArray(data.data || []);
            model.dialogModel.billModel.payType(data.data[0] || []);
            model.searchModel.payType(data.data[0] || []);
        });
        dictionaryService.findAll('search_EQ_parent.id='+autoConfig.bill.reliefReason).success(function(data){
            model.dialogModel.reliefReasonArray(data.data || []);
            model.dialogModel.billModel.reliefReason(data.data[0] || []);
        });

        /**
         * 查询
         */
        model.searchData = function(data, event){
            var startDay = +moment(data.searchModel.checkOutDate());
            var endDay = +moment(data.searchModel.checkOutEndDate());
            var param = '';
            if(startDay > 0 && endDay > 0) {
                param += 'search_GTE_checkOutDate=' + startDay + '&';
                param += 'search_LT_checkOutDate=' + ( endDay + 24 * 3600 * 1000) + '&';
            }
            param += 'search_LIKE_checker=' + data.searchModel.checker() + '&';
            param += 'search_EQ_payType.id=' + data.searchModel.payType().id;
            if(initParam) {
                param = initParam + '&' + param;
            }
            data.tableModel.param(param);
            model.tableModel.loadData(true);
        };

        /**
         * 详细
         */
        model.showData = function (data, event) {
            var viewModel = new Model().dialogModel;
            viewModel.billModel = common.mappingData(common.extend(viewModel.billModel, data));

            var checkOutDateModel = viewModel.billModel.checkOutDate;
            common.dealDateBeforeShow(checkOutDateModel, "yyyy-MM-dd hh:mm:ss");
            var dia = dialog({
                id: "showBillDialog",
                url: '/mdSheQuHospitalBase/bill/bill.show.tmpl.html',
                width: '600px',
                title: '账单表基本信息',
                okValue: '关闭',
                ok: function () {
 
                },
                onshow: function () {
 
                }
            });
            common.showModalDialog(dia, viewModel);
        };

        /**
         * 增加
         */
        model.addData = function(){
            var viewModel = new Model().dialogModel;
            viewModel.payTypeArray(model.dialogModel.payTypeArray());
            viewModel.reliefReasonArray(model.dialogModel.reliefReasonArray());
            viewModel.billModel.payType(model.dialogModel.billModel.payType());
            viewModel.billModel.reliefReason(model.dialogModel.billModel.reliefReason());
            modifyData(viewModel, '新建');
            model.tableModel.loadData(true);
        };
        /**
         * 增加
         */
        model.dateChange = function(data, event){

        };

        /**
         * 修改
         */
        model.updateData = function (data, event) {
            var viewModel = new Model().dialogModel;
            viewModel.billModel = common.mappingData(common.extend(viewModel.billModel, data), []);
            modifyData(viewModel, '编辑');
        };

        /**
         * 增加或修改
         */
        function modifyData(viewModel, titlePrefix){

            var dia = dialog({
                id: 'saveBillDialog',
                url: '/mdSheQuHospitalBase/bill/bill.save.tmpl.html',
                width: '600px',
                title: titlePrefix + '账单表',
                okValue: '确定',
                cancelValue: '取消',
                ok: function () {
                    if(common.isErrors(viewModel.billModel)){
                        var param = ko.mapping.toJSON(viewModel.billModel);
                        service.saveData(param).success(function(data){
                            common.showAlertDialog('数据保存成功!', 'success');
                            model.tableModel.loadData(true);
                        });
                    }else{
                        return false;
                    }
                },
                cancel: function () {},
                onshow: function(){
                    // 监听总应收变化
                    viewModel.billModel.receiveTotal.subscribe(function (data) {
                        changeBalanace(viewModel,data,1);
                    });
                    // 监听总实收变化
                    viewModel.billModel.actTotal.subscribe(function (data) {
                        changeBalanace(viewModel,data,2);
                    });
                    // 监听总实收变化
                    viewModel.billModel.actChange.subscribe(function (data) {
                        changeBalanace(viewModel,data,3);
                    });
                }
            });
            common.showModalDialog(dia, viewModel);
        }

        /**
         * 将找零和减免平衡
         */
        function changeBalanace(viewModel,data,status){
            //console.log(viewModel.billModel.actTotal()+"||"+data);
            var receive,act=0;
            if(status===1){
                receive = data;
                act = viewModel.billModel.actTotal();
            }else if(status===2){
                act=data;
                receive=viewModel.billModel.receiveTotal();
            } else {
                act = viewModel.billModel.actTotal();
                receive=viewModel.billModel.receiveTotal();
            }
            if (status!=3) {
                viewModel.billModel.actChange(common.accSub(act, receive));
            }
            //console.log(act+"||"+receive+"|、、"+viewModel.billModel.actChange()+"||"+common.accSub(act,viewModel.billModel.actChange())+"----"+common.accSub(receive,common.accSub(act,viewModel.billModel.actChange())));
            viewModel.billModel.reliefTotal(common.accSub(receive,common.accSub(act,viewModel.billModel.actChange())));
        }

        /**
         * 删除
         */
        model.deleteData = function (data, event) {
            common.showConfirmDialog('确定要删除吗?', function () {
                service.deleteDataById(data.id).success(function(data){
                    common.showAlertDialog('数据删除成功!', 'success');
                    model.tableModel.loadData(true);
                });
            });
        };

        return model;
    }
    return {
        initialize: initialize
    };
});